// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.globalObjects;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import se.krka.kahlua.vm.KahluaTable;
import se.krka.kahlua.vm.KahluaTableIterator;
import zombie.Lua.LuaEventManager;
import zombie.Lua.LuaManager;
import zombie.core.Core;
import zombie.debug.DebugLog;
import zombie.network.TableNetworkUtils;
import zombie.util.Type;

public final class CGlobalObjects {
    protected static final ArrayList<CGlobalObjectSystem> systems = new ArrayList();
    protected static final HashMap<String, KahluaTable> initialState = new HashMap();

    public static void noise(String message) {
        if (Core.bDebug) {
            DebugLog.log("CGlobalObjects: " + message);
        }
    }

    public static CGlobalObjectSystem registerSystem(String name) {
        CGlobalObjectSystem cGlobalObjectSystem = getSystemByName(name);
        if (cGlobalObjectSystem == null) {
            cGlobalObjectSystem = newSystem(name);
            KahluaTable kahluaTable0 = (KahluaTable)initialState.get(name);
            if (kahluaTable0 != null) {
                KahluaTableIterator kahluaTableIterator0 = kahluaTable0.iterator();

                while (kahluaTableIterator0.advance()) {
                    Object object0 = kahluaTableIterator0.getKey();
                    Object object1 = kahluaTableIterator0.getValue();
                    if ("_objects".equals(object0)) {
                        KahluaTable kahluaTable1 = Type.tryCastTo(object1, KahluaTable.class);
                        int int0 = 1;

                        for (int int1 = kahluaTable1.len(); int0 <= int1; int0++) {
                            KahluaTable kahluaTable2 = Type.tryCastTo(kahluaTable1.rawget(int0), KahluaTable.class);
                            int int2 = ((Double)kahluaTable2.rawget("x")).intValue();
                            int int3 = ((Double)kahluaTable2.rawget("y")).intValue();
                            int int4 = ((Double)kahluaTable2.rawget("z")).intValue();
                            kahluaTable2.rawset("x", null);
                            kahluaTable2.rawset("y", null);
                            kahluaTable2.rawset("z", null);
                            CGlobalObject cGlobalObject = Type.tryCastTo(cGlobalObjectSystem.newObject(int2, int3, int4), CGlobalObject.class);
                            KahluaTableIterator kahluaTableIterator1 = kahluaTable2.iterator();

                            while (kahluaTableIterator1.advance()) {
                                cGlobalObject.getModData().rawset(kahluaTableIterator1.getKey(), kahluaTableIterator1.getValue());
                            }
                        }

                        kahluaTable1.wipe();
                    } else {
                        cGlobalObjectSystem.modData.rawset(object0, object1);
                    }
                }
            }
        }

        return cGlobalObjectSystem;
    }

    public static CGlobalObjectSystem newSystem(String name) throws IllegalStateException {
        if (getSystemByName(name) != null) {
            throw new IllegalStateException("system with that name already exists");
        } else {
            noise("newSystem " + name);
            CGlobalObjectSystem cGlobalObjectSystem = new CGlobalObjectSystem(name);
            systems.add(cGlobalObjectSystem);
            return cGlobalObjectSystem;
        }
    }

    public static int getSystemCount() {
        return systems.size();
    }

    public static CGlobalObjectSystem getSystemByIndex(int index) {
        return index >= 0 && index < systems.size() ? (CGlobalObjectSystem)systems.get(index) : null;
    }

    public static CGlobalObjectSystem getSystemByName(String name) {
        for (int _int = 0; _int < systems.size(); _int++) {
            CGlobalObjectSystem cGlobalObjectSystem = (CGlobalObjectSystem)systems.get(_int);
            if (cGlobalObjectSystem.name.equals(name)) {
                return cGlobalObjectSystem;
            }
        }

        return null;
    }

    public static void initSystems() {
        LuaEventManager.triggerEvent("OnCGlobalObjectSystemInit");
    }

    public static void loadInitialState(ByteBuffer bb) throws IOException {
        byte _byte = bb.get();

        for (int _int = 0; _int < _byte; _int++) {
            String string = zombie.GameWindow.ReadStringUTF(bb);
            if (bb.get() != 0) {
                KahluaTable kahluaTable = LuaManager.platform.newTable();
                initialState.put(string, kahluaTable);
                TableNetworkUtils.load(kahluaTable, bb);
            }
        }
    }

    public static boolean receiveServerCommand(String string0, String string1, KahluaTable kahluaTable) {
        CGlobalObjectSystem cGlobalObjectSystem = getSystemByName(string0);
        if (cGlobalObjectSystem == null) {
            throw new IllegalStateException("system '" + string0 + "' not found");
        } else {
            cGlobalObjectSystem.receiveServerCommand(string1, kahluaTable);
            return true;
        }
    }

    public static void Reset() {
        for (int _int = 0; _int < systems.size(); _int++) {
            CGlobalObjectSystem cGlobalObjectSystem = (CGlobalObjectSystem)systems.get(_int);
            cGlobalObjectSystem.Reset();
        }

        systems.clear();
        initialState.clear();
        CGlobalObjectNetwork.Reset();
    }
}
